package demo.propertySupport;

/**
 * description: <br>
 * createTime: 2021/1/13 16:53 <br>
 *
 * @author zzh
 */

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.io.Serializable;

public class ProBean implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String name;

    private PropertyChangeSupport pcs;

    private VetoableChangeSupport vcs;


    public ProBean(String name) {
        this.name = name;
        pcs = new PropertyChangeSupport(this);
        vcs = new VetoableChangeSupport(this);
    }


    public ProBean() {
        pcs = new PropertyChangeSupport(this);
        vcs = new VetoableChangeSupport(this);
    }


    public String getName() {
        return name;
    }


    //值被改变后，触发监听器
    public void setName(String name) throws PropertyVetoException {
        String oldValue = this.getName();
        String newValue = name;
        //        System.out.println("######SetName######");
        vcs.fireVetoableChange("name", oldValue, newValue);
        pcs.firePropertyChange("name", oldValue, newValue);
        this.name = name;
    }


    //监听器的添加/删除方法
    public void addPropertyChangeListener(PropertyChangeListener Listener) {
        pcs.addPropertyChangeListener(Listener);
    }


    public void addVetoableChangeListener(VetoableChangeListener Listener) {
        vcs.addVetoableChangeListener(Listener);
    }


    public void removePropertyChangeListener(PropertyChangeListener Listener) {
        pcs.removePropertyChangeListener(Listener);
    }


    public void removeVetoableChangeListener(VetoableChangeListener Listener) {
        vcs.removeVetoableChangeListener(Listener);
    }
}